$("#address").on("click",function(){
	$(window).attr('location',"./addresslist.html");
})
var perfix = "http://1.15.177.112:8080";
//点击返回
$("#back").on("click",function(){
	$(window).attr('location',"../index.html");
})
// --------- 用户数据 ---------
var userId = localStorage.getItem("userId");
var token = localStorage.getItem("token");
console.log(userId);
if(token == null ){
	$(".bg-login").text("登录/注册")
	$(".bg-login").on('click',function(){
		$(window).attr('location',"./login.html");
	})
}else{
	var username = localStorage.getItem("username");
	var pic = localStorage.getItem("userPic");
	if(pic == null){
		$("#pic").attr('src',"../image/bg-pic.png");
	}else{
		$("#pic").attr('src',pic);
	}
	$(".bg-login").text(username)	
}
// ---------- 上传头像 ----------
$("#pic").on('click',function(){
	$("#upload").click();
})

//上传头像
$("#upload").on('change',function(){
	var formData = new FormData;
	formData.append("file", $("#upload")[0].files[0]);
	
	//上传图片
	$.ajax({
		headers: {
			'Authorization': localStorage.getItem("token")
		},
		url: perfix + '/upload/doUpLoad',
		type: 'post',
		cache: false, //上传文件不需要缓存
		data: formData,
		processData: false, // 告诉jQuery不要去处理发送的数据
		contentType: false, // 告诉jQuery不要去设置Content-Type请求头
		success: function(data) {
			var user = {
				"id":localStorage.getItem("userId"),
				"userImg":data.data
			}
			update(user);
			localStorage.setItem("userPic",data.data);
		}
	})
})

// ---------- 上传头像 ----------

$("#myOrder").on('click',function(){
	$(window).attr('location',"./order.html");
})


//调用用户更新接口
function update(user){
	$.ajax({
		headers: {
			'Authorization': localStorage.getItem("token")
		},
		url: perfix + '/user/update',
		type: 'post',
		data:JSON.stringify(user),
		contentType: 'application/json',
		success:function(data){
			location.reload();
		}
	})
}

//------- 退出登录  -------
$("#outLogin").on('click',function(){
	if(localStorage.getItem("token") == null){
		return;
	}
	localStorage.removeItem("username");
	localStorage.removeItem("userPic");
	localStorage.removeItem("userId");
	localStorage.removeItem("token");
	location.reload();
	$(window).attr('location',"./login.html");
})
//------- 退出登录  -------